package com.ssyt.examinationsystem.controller;

import com.ssyt.examinationsystem.entity.RoleInfo;
import com.ssyt.examinationsystem.service.IRoleInfoService;
import com.ssyt.examinationsystem.service.impl.RoleInfoServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.List;
import java.util.Map;

/**
 * Servlet implementation class RoleInfoController
 */
public class RoleInfoController extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public RoleInfoController() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
     * response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doPost(request, response);
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
     * response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // 创建一个查询所有角色方法业务方法对象
        IRoleInfoService roleService = new RoleInfoServiceImpl();
        PrintWriter out = response.getWriter();
        // 调用地址栏上的参数
        String flag = request.getParameter("flag");
        if ("query".equals(flag)) {
            query(roleService, request, response);
        } else if ("initAdd".equals(flag)) {
            initAdd(roleService, request, response);
        } else if ("add".equals(flag)) {
            add(roleService, request, response, out);
        } else if ("delete".equals(flag)) {
            dalete(roleService, request, response, out);
        } else if ("initUpdate".equals(flag)) {
            initUpdate(roleService, request, response);
        } else if ("update".equals(flag)) {
            update(roleService, request, response, out);
        }

        out.flush();
        out.close();
    }

    // 修改角色信息的方法
    private void update(IRoleInfoService roleService, HttpServletRequest request, HttpServletResponse response,
                        PrintWriter out) throws UnsupportedEncodingException {
        // 创建一个角色对象，并封装在修改页面传过来的表单数据
        RoleInfo role = new RoleInfo();
        Integer roleId = Integer.valueOf(request.getParameter("roleId"));
        role.setRoleId(roleId);

        String roleName = request.getParameter("roleName");
        roleName = URLDecoder.decode(roleName, "UTF-8");
        role.setRoleName(roleName);
        // 调用修改的业务方法
        int row = roleService.updateRoleInfo(role);
        // 判断是否修改成功
        if (row > 0) {
            out.print("ok");
        }
    }

    /**
     * 初始化修改的方法
     *
     * @param roleService
     * @param request
     * @param response
     * @throws IOException
     * @throws ServletException
     */
    private void initUpdate(IRoleInfoService roleService, HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // 获取当前修改角色的编号
        String roleId = request.getParameter("roleId");
        Map<String, Object> role = roleService.getRoleByRoleId(roleId);
        request.setAttribute("role", role);

        request.getRequestDispatcher("WEB-INF/views/role/update.jsp").forward(request, response);
    }

    /**
     * 删除角色信息的方法
     *
     * @param roleService
     * @param request
     * @param response
     * @param out
     */
    private void dalete(IRoleInfoService roleService, HttpServletRequest request, HttpServletResponse response,
                        PrintWriter out) {
        // 获取删除角色的编号
        String roleId = request.getParameter("roleId");
        // 调用删除角色信息的业务方法
        int row = roleService.deleteRoleInfo(roleId);
        if (row > 0) {
            out.print("ok");
        }
    }

    /**
     * 添加角色的方法
     *
     * @param roleService
     * @param request
     * @param response
     * @param out
     * @throws IOException
     * @throws ServletException
     */
    private void add(IRoleInfoService roleService, HttpServletRequest request, HttpServletResponse response,
                     PrintWriter out) throws ServletException, IOException {
        // 获取添加的表单数据
        String roleName = request.getParameter("roleName");
        // 对网页数据进行转码
        roleName = URLDecoder.decode(roleName, "UTF-8");
        RoleInfo role = new RoleInfo();
        role.setRoleName(roleName);
        // 调用添加角色信息的业务方法
        int row = roleService.addRoleInfo(role);
        if (row > 0) {
            out.print("ok");
        }
    }

    /**
     * 初始化添加角色的方法
     *
     * @param roleService
     * @param request
     * @param response
     * @throws IOException
     * @throws ServletException
     */
    private void initAdd(IRoleInfoService roleService, HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        request.getRequestDispatcher("WEB-INF/views/role/add.jsp").forward(request, response);
    }

    /**
     * 查询所有角色的方法
     *
     * @param roleService
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    private void query(IRoleInfoService roleService, HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // 调用角色的查询所有业务方法
        List<RoleInfo> roleList = roleService.getRoleInfos();
        // 将集合保存在请求对象中
        request.setAttribute("roleList", roleList);
        // 转发到WEB-INF/login.jsp页面
        request.getRequestDispatcher("WEB-INF/views/role/list.jsp").forward(request, response);
    }

}
